﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace AutoDeployForSims.Utility
{
	public class EnumToDescription : IValueConverter
	{
		public EnumToDescription()
		{

		}

		private string GetEnumDescription(Enum enumObj)
		{
			FieldInfo fieldInfo = enumObj.GetType().GetField(enumObj.ToString());
			var descriptionAttr = fieldInfo
				.GetCustomAttributes(false)
				.OfType<DescriptionAttribute>()
				.Cast<DescriptionAttribute>()
				.SingleOrDefault();
			if (descriptionAttr == null)
			{
				return enumObj.ToString();
			}
			else
			{
				return descriptionAttr.Description;
			}
		}

		object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			Enum myEnum = (Enum)value;
			string description = GetEnumDescription(myEnum);
			return description;
		}

		object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			return string.Empty;
		}
	}

	public class CommEnumDes
	{
		private string GetEnumDescription(Enum enumObj)
		{
			FieldInfo fieldInfo = enumObj.GetType().GetField(enumObj.ToString());

			object[] attribArray = fieldInfo.GetCustomAttributes(false);

			if (attribArray.Length == 0)
			{
				return enumObj.ToString();
			}
			else
			{
				DescriptionAttribute attrib = attribArray[0] as DescriptionAttribute;
				return attrib.Description;
			}
		}
		public List<string> GetEnumDesLstByEnumTypeName(Type tEnumName)
		{
			List<string> comboItemLst = new List<string>();

			foreach (var item in tEnumName.GetEnumValues())
			{
				comboItemLst.Add(GetEnumDescription((Enum)item));
			}
			return comboItemLst;
		}

	}
}
